﻿
#include "avatar_id.h"
#include <EASTL/array.h>
#include <enum_parser.h>

namespace ifire::game {

AvatarGroup::AvatarGroup(ConfigManager* manager, const ikit::XmlNode& node)
    : ConfigInfo(manager, node("Name")) {
  // 第一个项目必须是Root
  items_.emplace_back(0, "Root");

  // 读取到项目
  node.Foreach("Item", [this](const ikit::XmlNode& n, int idx) {
    items_.emplace_back(n, idx + 1); // 注意这里的Idx+1
  });

  // 遍历项目，进行印射
  for (int i = 0; i < items_.size(); i++) {
    auto& item = items_[i];
    map_[item.name.uid()] = i;
  }

  // 设置核心骨骼ID
  auto& key = node["KeyBone"];
  head_id = GetID(key("Head"));
  spine_id = GetID(key("Spine"));
  neck_id = GetID(key("Neck"));
  hips_id = GetID(key("Hips"));
  left_foot_id = GetID(key("LeftFoot"));
  right_foot_id = GetID(key("RightFoot"));
  left_toes_id = GetID(key("LeftToes"));
  right_toes_id = GetID(key("RightToes"));
  left_hand_id = GetID(key("LeftHand"));
  right_hand_id = GetID(key("RightHand"));
}

int AvatarGroup::GetID(ikit::sstr name) const {
  if (name.empty()) {
    return AVATAR_NONE_ID;
  }

  auto it = map_.find(name.uid());
  if (it != map_.end()) {
    return it->second;
  }
  return AVATAR_NONE_ID;
}

bool AvatarGroup::IsFootFix(int id) const {
  return id == left_foot_id || id == right_foot_id || id == left_toes_id ||
         id == right_toes_id;
}

AvatarBoneID::AvatarBoneID(int _index, ikit::sstr _name)
    : index(_index), name(_name), fix_direction(false) {}

AvatarBoneID::AvatarBoneID(const ikit::XmlNode& node, int _index)
    : index(_index)
    , name(node("Name"))
    , fix_direction(node("FixDirection", false)) {}

} // namespace ifire::game